using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class DataManager : MonoBehaviour
{
    public float musicSetting;
    public float SFXSetting;
    public static DataManager Instance { get; private set; }
    private void Awake()
    {
        if(Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }

    [Serializable]
    class SaveData
    {
        public float musicSetting;
        public float SFXSetting;
    }

    public void SaveSettings()
    {
        SaveData data = new SaveData();
        data.musicSetting = musicSetting;
        data.SFXSetting = SFXSetting;
        
        string json = JsonConvert.SerializeObject(data);
        File.WriteAllText(Application.persistentDataPath + "/Settings.json",json);
        print(Application.persistentDataPath);
    }

    public void LoadSettings()
    {
        string path = Application.persistentDataPath + "/Settings.json";
        if(File.Exists(path))
        {
            
            string json = File.ReadAllText(path);
            SaveData data = JsonConvert.DeserializeObject<SaveData>(json);
            musicSetting = data.musicSetting;
            SFXSetting = data.SFXSetting;
        }
        else
        {
            musicSetting = 0.5f;
            SFXSetting = 0.5f;
        }
    }
}
